package controller;

import javafx.fxml.Initializable;
import ui.Main;
import common.Message;
import common.MessageType;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.*;
import javafx.scene.layout.Pane;
import util.*;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ResourceBundle;

public class RegisterController implements Initializable {
    @FXML
    private Button next;
    @FXML
    private TextField email;
    @FXML
    private PasswordField password;
    @FXML
    private PasswordField surepwd;
    @FXML
    private TextField phone;
    @FXML
    private DatePicker birth;
    private SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");//日期格式
    @FXML
    private Label error1;
    @FXML
    private Label error2;//邮箱格式错误
    @FXML
    private Label error3;//密码
    @FXML
    private Label error;//验证码
    @FXML
    private TextField code;
    @FXML
    private TextField username;
    @FXML
    private Button getCodeButton;
    private long lastClickTime;
    @FXML
    public void getCode(){
        if(email.getText().equals("")){
            error2.setText("邮箱为空，请填写");
            return;
        }
        if(!EmailCheck.checkEmail(email.getText())) {
            error2.setText("邮箱格式错误");
            return;
        }
        getCodeButton.setOnMouseClicked(event -> {
            if (System.currentTimeMillis() - lastClickTime > 60000) { // 按钮上一次被点击的时间距离当前时间已经超过 60 秒
                // 执行发送验证码操作
                Socket socket = null;
                try {
                    socket=new Socket("127.0.0.1",10086);
                    Message ms=new Message();
                    ms.setState(MessageType.Email_Send);
                    ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
                    oos.writeObject(ms);
                    oos.writeObject(email.getText());
                } catch (Exception e) {
                    System.out.println("获取邮箱出错");
                    e.printStackTrace();
                }finally {
                    try {
                        if (socket != null) {
                            socket.close();
                        }
                    } catch (Exception e) {
                        System.out.println("关闭socket出错");
                        e.printStackTrace();
                    }
                }
                // 更新时间戳，禁用按钮并开始倒计时计时器
                lastClickTime = System.currentTimeMillis();
                getCodeButton.setDisable(true);
                StartCountdownTimer.startCountdownTimer(getCodeButton,60);//倒计时
            }
        });
    }
    //下一步
    @FXML
    public void toNext(){
        //需要检查格式
        if(email.getText().equals("")||username.getText().equals("")||phone.getText().equals("")||birth.getValue()==null||password.getText().equals("")||surepwd.getText().equals("")|| code.getText().equals("")){
            error.setText("未填写完毕");
            return;
        }
        if(!surepwd.getText().equals(password.getText())){
            error3.setText("两次密码不一致");
            return;
        }
        if(!PhoneCheck.checkPhone(phone.getText())){
            error1.setText("手机号格式错误");
            return;
        }
        error1.setText("手机号格式正确");
        error2.setText("邮箱格式正确");
        error3.setText("两次密码一致");//相等且非空
        if(Main.userClientService.UserRegister(email.getText(), MD5.getPWD(password.getText()), code.getText(),username.getText(),phone.getText(),String.valueOf(birth.getValue()))){
            System.out.println(String.valueOf(birth.getValue()));
            error.setText("邮箱可用且验证码正确");
            System.out.println("注册成功，即将跳转至主界面");
            try {
                Pane root= FXMLLoader.load(getClass().getResource("/Interface/MainInterface.fxml"));
                ToMain.toMain(next,root);
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("跳转至主界面失败");
            }
        }
        else{
            error.setText("邮箱已被使用/验证码错误");
        }
    }
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        SetDatePicker.setDatePicker(birth);
    }
}
